package pl.smath.expression;

import java.util.Iterator;

public class SumTerm extends MultiTerm{

	/**
	 * constructor
	 */
	public SumTerm() {
		super("+");
	}
	
	/**
	 * @return tekst wyrażenia
	 */
	public Term copy(){
		SumTerm term = new SumTerm();
		
		for(int i = 0; i < getTermsList().size(); i++){
			term.addTerm(getTermsList().get(i).copy());
		}
		
		term.setNegative(isNegative());
		return term;
	}

	
	/**
	 * Zamiana do stringa
	 */
	public String toString(){
		String output = "";
		Iterator<Term> terms = getTerms();
		
		
		while(terms.hasNext()){
			Term term = terms.next();
			
			if(output.length() > 0){
				if(!term.isNegative()){
					output += "+";
				}
			}

			if(term instanceof SumTerm && !term.isNegative())
				output += "(" + term.toString() + ")";
			else
				output += term.toString();
		}

		if(isNegative())
			output = "-(" + output + ")";
		
		return output;
	}
}
